<template>
  <aside class="sidebar">
    <!-- 热门标签 -->
    <div class="sidebar-widget">
      <h3 class="widget-title">热门标签</h3>
      <div class="tags-cloud">
        <span
          v-for="tag in hotTags"
          :key="tag.id"
          class="tag-item"
          :class="{ active: selectedTag === tag.name }"
          @click="handleTagClick(tag)"
        >
          {{ tag.name }}
        </span>
      </div>
    </div>

    <!-- 广告位 -->
    <div class="sidebar-widget ad-widget">
      <div class="ad-placeholder">
        <p>广告位</p>
        <span>300 x 250</span>
      </div>
    </div>

    <!-- 推荐阅读 -->
    <div class="sidebar-widget">
      <h3 class="widget-title">推荐阅读</h3>
      <div v-if="recommendedArticles.length > 0" class="recommended-list">
        <NuxtLink
          v-for="(item, index) in recommendedArticles"
          :key="item.id"
          :to="`/article/${item.id}`"
          class="recommended-item"
        >
          <span class="rank" :class="{ top: index < 3 }">{{ index + 1 }}</span>
          <div class="recommended-info">
            <p class="recommended-title">{{ item.title }}</p>
            <span class="recommended-views">
              <IconEye class="icon" />
              {{ item.readCounts || 0 }}
            </span>
          </div>
        </NuxtLink>
      </div>
      <!-- 暂无推荐样式 -->
      <div v-else class="empty-recommended">
        <div class="empty-icon">📚</div>
        <h4 class="empty-title">暂无推荐</h4>
        <p class="empty-desc">精彩内容即将上线，敬请期待</p>
      </div>
    </div>
  </aside>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import IconEye from '~/assets/icons/IconEye.vue'
import { getTagsList, getHomeList } from '~/api/article'
import storage from '@/utils/storage'
interface Tag {
  id: number
  name: string
  count?: number
}

interface RecommendedArticle {
  id: number
  title: string
  readCounts?: number
  views?: number
}

const props = withDefaults(
  defineProps<{
    selectedTag?: string
  }>(),
  {
    selectedTag: '',
  },
)

const emit = defineEmits<{
  tagClick: [tagName: string]
}>()

// 热门标签数据
const hotTags = ref<Tag[]>([])
// 推荐阅读数据
const recommendedArticles = ref<RecommendedArticle[]>([])

// 获取分类列表
const fetchTagsList = async () => {
  try {
    const { data: response } = await getTagsList()
    const data = response.value
    if (data.code === 200) {
      hotTags.value = data.data.map((tag: any) => ({
        id: tag.id,
        name: tag.name,
        color: tag.color || '#667eea',
      }))
      storage.set('hotTags', hotTags.value)
    }
  } catch (error) {
    console.error('获取分类列表失败:', error)
  }
}

// 获取精选文章
const fetchRecommendedArticles = async () => {
  try {
    const { data: response } = await getHomeList({
      isHot: 1, // 获取热门文章
      isTop: 0,
      page: 1,
      size: 5,
    })
    const data = response.value
    if (data.code === 200) {
      recommendedArticles.value = data.data.list || []
    }
  } catch (error) {
    console.error('获取精选文章失败:', error)
  }
}

// 标签点击事件
const handleTagClick = (tag: Tag) => {
  // 跳转到全部文章页面，并选中对应的搜索标签
  navigateTo({
    path: '/articles',
    query: { categoryId: tag.id },
  })
}

// 组件挂载时获取数据
onMounted(() => {
  fetchTagsList()
  fetchRecommendedArticles()
})
</script>

<style scoped>
.sidebar {
  width: 320px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.sidebar-widget {
  background: white;
  border-radius: 12px;
  padding: 20px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
}

.widget-title {
  font-size: 18px;
  font-weight: 700;
  color: #1a202c;
  margin: 0 0 16px 0;
  padding-bottom: 12px;
  border-bottom: 2px solid #f7fafc;
  position: relative;
}

.widget-title::after {
  content: '';
  position: absolute;
  bottom: -2px;
  left: 0;
  width: 40px;
  height: 2px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

/* 热门标签 */
.tags-cloud {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.tag-item {
  padding: 6px 14px;
  background: #f7fafc;
  border-radius: 6px;
  font-size: 13px;
  color: #4a5568;
  cursor: pointer;
  transition: all 0.3s;
}

.tag-item:hover {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  transform: translateY(-2px);
}

.tag-item.active {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
}

.tag-item em {
  font-style: normal;
  opacity: 0.7;
  font-size: 12px;
}

/* 广告位 */
.ad-widget {
  background: #f7fafc;
}

.ad-placeholder {
  width: 100%;
  height: 250px;
  background: linear-gradient(135deg, rgba(102, 126, 234, 0.1) 0%, rgba(118, 75, 162, 0.1) 100%);
  border: 2px dashed #cbd5e0;
  border-radius: 8px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  color: #a0aec0;
}

.ad-placeholder p {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
}

.ad-placeholder span {
  font-size: 14px;
}

/* 推荐阅读 */
.recommended-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.recommended-item {
  display: flex;
  gap: 12px;
  padding: 12px;
  border-radius: 8px;
  transition: all 0.3s;
  text-decoration: none;
}

.recommended-item:hover {
  background: #f7fafc;
}

.rank {
  width: 24px;
  height: 24px;
  border-radius: 4px;
  background: #e2e8f0;
  color: #718096;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: 700;
  flex-shrink: 0;
}

.rank.top {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
}

.recommended-info {
  flex: 1;
}

.recommended-title {
  font-size: 14px;
  color: #2d3748;
  margin: 0 0 4px 0;
  font-weight: 600;
  line-height: 1.4;
}

.recommended-views {
  font-size: 12px;
  color: #a0aec0;
  display: flex;
  align-items: center;
  gap: 4px;
}

.recommended-views .icon {
  width: 14px;
  height: 14px;
}

/* 暂无推荐样式 */
.empty-recommended {
  text-align: center;
  padding: 40px 20px;
  background: linear-gradient(135deg, rgba(102, 126, 234, 0.05) 0%, rgba(118, 75, 162, 0.05) 100%);
  border-radius: 8px;
  border: 2px dashed #e2e8f0;
}

.empty-icon {
  font-size: 48px;
  margin-bottom: 16px;
}

.empty-title {
  font-size: 18px;
  font-weight: 600;
  color: #1a202c;
  margin: 0 0 8px 0;
}

.empty-desc {
  font-size: 14px;
  color: #718096;
  margin: 0;
}

/* 响应式 */
@media (max-width: 968px) {
  .sidebar {
    width: 100%;
  }
}
</style>
